import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
import * as dotenv from 'dotenv';
dotenv.config();

async function bootstrap() {
  console.log('DATABASE_URL:', process.env.DATABASE_URL);
  console.log(
    'puppeteerskipdown',
    process.env.PUPPETEER_SKIP_CHROMIUM_DOWNLOAD,
  );
  console.log('puppeteerexecpath', process.env.PUPPETEER_EXECUTABLE_PATH);
  const envclients = process.env.CORS_CLIENTS;
  const clients = envclients?.split(',');
  console.log(clients);
  const serverUrl = process.env.SERVER_URL;
  console.log(`server url ${serverUrl}`);
  const app = await NestFactory.create(AppModule);

  app.enableCors({
    origin: clients, // Explicitly allow your Next.js app's origin
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS', // Allowed HTTP methods
    credentials: true, // Allow credentials (cookies, authorization headers, etc.)
  });
  const options = new DocumentBuilder()
    .setTitle('Doggy API document')
    .setDescription('Doggy api...')
    .setVersion('1.0')
    .addServer(serverUrl || 'http://127.0.0.1:3000', 'Local environment')
    // .addServer('https://staging.yourapi.com/', 'Staging')
    //.addServer('http://127.0.0.1:3001', 'Production')
    .addTag('DOGGY API TABLE')
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api-docs', app, document);
  app.useGlobalPipes(
    new ValidationPipe({
      transform: true, // Automatically transform query params to DTO types
      whitelist: true, // Strip unknown properties
    }),
  );
  await app.listen(process.env.PORT ?? 3001);
}
bootstrap();
